% Matlab code to visualize the equilibrium and illustrate the spirals

% To run this file seperately, specify the root directly below:
% cd 'C:/Users/Jilun/Dropbox/WorldEconomy_RiskIntolerance/paper RFS final submission/replication/numerical and calibration exercises'

% Make the figures for the baseline analysis without policy
%%% Make two figures:
%%% Figure 2: Illustrate the unique equilibrium and the effect of supply shocks/downward spirals
%%% Figure 3: Illustrate the possibility of multiplicity


clear all;
% Baseline parameters (will change later to illustrate multiplicity)
global g rho sigma PStar taub tauh k l tau tauBarBench;
global z0_1 z0_2;
loadVariables; % Sets the above parameters and the baseline shocks

eq{1} = solveEquilibrium(z0_1,0);
eq{2} = solveEquilibrium(z0_2,0);



%%%%%%%%%%%% FIGURE 2
%%%%%%%%%%%% Case with unique equilibrium

figure('Position',[30 30 830 430]);

% Plot the remaining curves
lineType.actual{1} = 'b--';
lineType.required{1} = 'r--';
lineType.actual{2} = 'b';
lineType.required{2} = 'r';


for i=1:2
% Plot each equilibria respectively
hold on;
if eq{i}.rf > 0
% The program returns the actual Sharpe ratio with rf=0
% When there is a unique corner solution
% make an adjustment to the Sharpe ratio to accommodate the fact that rf>0
sharpeActualArr = eq{i}.sharpeActualArr - eq{i}.rf/sigma;
legendStr{i*2-1} = sprintf('Actual Sharpe ratio, $r_0^f>0$');
elseif eq{i}.rf == 0
sharpeActualArr = eq{i}.sharpeActualArr;
legendStr{i*2-1} = sprintf('Actual Sharpe ratio, $r_0^f=0$');
end
sharpeRequiredArr = eq{i}.sharpeRequiredArr;
pArr=eq{i}.pArr;
ind = eq{i}.ind; % Index of the equilibrium
z0 = eq{i}.z0;

h(i*2-1) = plot(eq{i}.pArr,sharpeActualArr,lineType.actual{i},'LineWidth',3);
h(i*2) = plot(pArr,sharpeRequiredArr,lineType.required{i},'LineWidth',3);
legendStr{i*2} = sprintf('Required Sharpe ratio, $z_0=%1.2f$',z0);

% Mark ph on the figure
specText = sprintf('$p^h$');
text(ph,eq{1}.sharpeRequiredArr(end)+0.01,specText,'Interpreter','Latex','FontSize',20);
% Mark the equilibrium
if i==1 % corner equilibrium with efficient price
plot(pArr(ind),sharpeRequiredArr(ind),'ko-','LineWidth',3);    
else
plot(pArr(ind),sharpeActualArr(ind),'ko-','LineWidth',3);
end

end

titleStr = sprintf('Supply shocks with constrained interest rate');
title(titleStr,'Interpreter','Latex','FontSize',20);
xlabel('Normalized price, $p_0$','Interpreter','Latex','FontSize',20);

hL = legend(h([3 4 1 2]),legendStr{3},legendStr{4},legendStr{1},legendStr{2});
set(hL,'orientation','vertical','fontsize',15,'interpreter','latex');

xlim([ph 1]);
ylim([eq{1}.sharpeRequiredArr(end) eq{2}.sharpeRequiredArr(1)]);

saveas(gcf,[pwd '/figures/fig2.eps'],'epsc');



%%%%%%%%%%%% FIGURE 3
%%%%%%%%%%%% Case with multiple equilibria
% Load the relevant variables, shocks, and the equilibrium
global g rho sigma PStar taub tauh k l tau
global z0_M
loadVariables_M; % Resets the above parameters and loads the shock that leads to multiplicity

% Calculate the equilibrium
eq_M = solveEquilibrium(z0_M,0);
pArr = eq_M.pArr;
sharpeActualArr = eq_M.sharpeActualArr;
sharpeRequiredArr = eq_M.sharpeRequiredArr;
ph = eq_M.ph;

figure('Position',[30 30 830 430]);
hold on;
h(1) = plot(pArr,sharpeActualArr,'b','LineWidth',3);
h(2) = plot(pArr,sharpeRequiredArr,'r','LineWidth',3);
% Find and mark the multiple equilibria
indEq_1 = 1;
plot(pArr(indEq_1),sharpeActualArr(indEq_1),'ko-','LineWidth',3);
specText = sprintf('$L$');
text(pArr(indEq_1),sharpeActualArr(indEq_1)+0.01,specText,'Interpreter','Latex','FontSize',20);

indEq_2 = length(pArr);
plot(pArr(indEq_2),sharpeRequiredArr(indEq_2),'ko-','LineWidth',3);
specText = sprintf('$H$');
text(pArr(indEq_2),sharpeRequiredArr(indEq_2)+0.01,specText,'Interpreter','Latex','FontSize',20);

% Mark ph on the figure
specText = sprintf('$p^h$');
text(ph,sharpeRequiredArr(end)+0.015,specText,'Interpreter','Latex','FontSize',20);

titleStr = sprintf('Multiple equilibria');
title(titleStr,'Interpreter','Latex','FontSize',20);
xlabel('Normalized price, $p_0$','Interpreter','Latex','FontSize',20);

legendStr1 = sprintf('Actual Sharpe ratio, $r_0^f=0$');
legendStr2 = sprintf('Required Sharpe ratio, high initial leverage $l$');
hL = legend(h([1 2]),legendStr1,legendStr2);
set(hL,'orientation','vertical','fontsize',15,'interpreter','latex');
xlim([ph 1]);

yMin = sharpeRequiredArr(end);
yMax = sharpeRequiredArr(1);
ylim([yMin yMax]);

saveas(gcf,[pwd '/figures/fig3.eps'],'epsc');

